package com.lagou.task18;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

public class ThreadSleepTest extends Thread {
    private boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        //每隔一秒钟获取一次系统时间
        while (flag){
            Date d1 = new Date();
            LocalDateTime l1 = LocalDateTime.now();
            SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            System.out.println("当前时间"+sdf.format(d1));
            try {
                //父类没有抛出，子类不能抛出更大的异常，
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadSleepTest t1 = new ThreadSleepTest();
        t1.start();
        //主线成等待5秒后结束子线程
        Thread.sleep(5000);
        t1.setFlag(false);
//        t1.stop();

    }
}
